package com.igeek.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.igeek.mapper.InsuranceMapper;
import com.igeek.model.claims.Claims;
import com.igeek.model.insurance.Insurance;
import com.igeek.model.insurance.Recording;
import com.igeek.service.IClaimsService;
import com.igeek.service.IInsuranceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2024-08-22
 */
@Service
public class InsuranceServiceImpl extends ServiceImpl<InsuranceMapper, Insurance> implements IInsuranceService {

    @Autowired
    private IClaimsService claimsService;

    @Override
    public Insurance getMoney(Integer id) {
        Insurance insurance=this.getById(id);
        Claims claims=claimsService.getById(insurance.getClaimsId());
        insurance.setExpenditure(claims.getLoss());
        this.baseMapper.updateById(insurance);
        return insurance;
    }

    @Override
    public Insurance getIncome(Insurance insurance, Integer id) {
        Insurance insurance1=this.getById(id);
        insurance1.setRate(insurance.getRate());
        insurance1.setIncome(insurance1.getMoney()-insurance1.getExpenditure()*insurance.getRate().doubleValue());
        insurance1.setExpenditure(insurance1.getExpenditure()*insurance.getRate().doubleValue());
        insurance1.setComment(insurance.getComment());
        this.baseMapper.updateById(insurance1);
        List<Recording> recordingList=new ArrayList<>();


       Recording data = new Recording();
            data.setId(id);
            data.setMonth(insurance1.getMonth());
            data.setExpenditure(insurance1.getExpenditure());
            data.setIncome(insurance1.getIncome());
            recordingList.add(data);
            String fileName = "E:\\item\\保险.xlsx";
            File file = new File(fileName);
            File tempFile = new File("E:\\item\\1.xlsx");
        if (file.exists()){
            // 第二次按照原有格式，不需要表头，追加写入
            EasyExcel.write(file,Recording.class).needHead(false).
                    withTemplate(file).file(tempFile).sheet().doWrite(recordingList);
        }else {
            // 第一次写入需要表头
            EasyExcel.write(file,Recording.class).sheet().doWrite(recordingList);
        }


        if (tempFile.exists()){
            file.delete();
            tempFile.renameTo(file);
        }


//            EasyExcel.write(fileName,Recording.class).sheet("出入账信息").doWrite(recordingList);

        return insurance1;
    }

    @Override
    public void export(HttpServletResponse response) {
        try {
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");

            String fileName = URLEncoder.encode("出入账记录", "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename="+ fileName + ".xlsx");
            String file="E:\\item\\保险.xlsx";
            try (
                    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
                    BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream());
            ){
                byte[] bytes = new byte[1024];
                int len ;
                while ((len=bis.read(bytes))!=-1){
                    bos.write(bytes , 0 , len);
                    bos.flush();
                }

            } catch (IOException e) {
                log.error("下载失败！");
                throw new RuntimeException(e);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
